package com.zhsj.OkeHomeConsumption.ui.fragment.minehome;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.ui.activity.PayAmountActivity;
import com.zhsj.OkeHomeConsumption.ui.fragment.minehome.mvp.BailPresenter;
import com.zhsj.OkeHomeConsumption.ui.fragment.minehome.mvp.BailUIInterFace;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;

/**
 * author : Xingdx
 * <p>
 * time   : 2018/10/18
 * desc   : 保证金
 */
public final class BailActivity extends MyActivity implements BailUIInterFace {

    @BindView(R.id.price_text)
    TextView priceText;
    @BindView(R.id.jiaona_button)
    Button jiaonaButton;

    private BailPresenter mBailPresenter;

    private String deposit;//最高保证金 ,
    private String depositLow;//最低保证金 ,
    private String depositRate;//每笔抽成比例 ,

    //用户信息实体
    private FormalUserInfo userInfo = null;

    private String lifetype = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bail;
    }

    @Override
    protected int getTitleId() {
        return R.id.tb_copy_title;
    }

    @Override
    protected void initView() {
        userInfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        mBailPresenter = new BailPresenter(getActivity(), this);
    }

    @Override
    protected void initData() {
        if(userInfo.getLifeUserDTO().getLifeType()==0){
            lifetype = getIntent().getStringExtra("usertype");
        }
        mBailPresenter.BailGet(userInfo.getLifeUserDTO().getApplyCopartnerType()==0?userInfo.getLifeUserDTO().getLifeType():userInfo.getLifeUserDTO().getApplyCopartnerType());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.jiaona_button)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jiaona_button:
                //pay_flage 标识支付类型（0 支付保证金）
//
                startActivity(new Intent(this,SecurityDepositActivity.class)
                        .putExtra("depositHigh",deposit)
                        .putExtra("depositLow",depositLow)
                        .putExtra("lifetype",lifetype)
                        .putExtra("depositRate",depositRate));
                finish();
                break;
        }
    }

    /**
     * 获取保证金
     */

    @Override
    public void BailGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsonarr = jsonOBJ.optJSONObject("data");
                // 数据对接正常
                deposit = jsonarr.optString("depositHigh");
                depositLow = jsonarr.optString("depositLow");
                depositRate = jsonarr.optString("depositRate");
                priceText.setText("¥" + deposit);
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            Log.e("BailGet", "获取保证金异常");
        }
    }
}